package com.allwees.bs.c.module.user.service.impl;

import com.google.common.collect.Maps;
import com.allwees.bs.c.module.product.entity.ProductEntity;
import com.allwees.bs.c.module.product.repository.ProductRepository;
import com.allwees.bs.c.module.user.service.IShareService;
import com.allwees.bs.core.modelbase.constant.ResultEnum;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

@Service
public class ShareServiceImpl implements IShareService {
    @Autowired
    private ProductRepository productRepository;

    @Value("${allwees.share.appId}")
    private String appId;

    @Override
    public Map<String, String> shareProduct(String url, String productUuid) {
        ProductEntity product = productRepository.getByUuid(productUuid);
        if (!Objects.nonNull(product)) {
            throw BusinessException.getInstance(ResultEnum.NOT_FOUND);
        }
        Map<String, String> map = Maps.newHashMap();
        map.put("appId", appId);
        map.put("title", product.getName());
        map.put("url", url);
        map.put("img", product.getMainPhoto());
        map.put("description", product.getDescription());
        return map;
    }
}
